<?php
        	
namespace Addons\StoreGis\Model;
use Home\Model\WeixinModel;
        	
/**
 * StoreGis的微信模型
 */
class WeixinAddonModel extends WeixinModel{
	function reply($dataArr, $keywordArr = array()) {
		$config = getAddonConfig ( 'StoreGis' ); // 获取后台插件的配置参数	
		//dump($config);
        $str = "该功能需要获取您当前的位置信息。请您按如下步骤操作：\n";
        $str .= "1.点击左下角【键盘】图标\n";
        $str .= "2.点击右下方的【+】号\n";
        $str .= "3.点击【位置】按钮，查看自己当前所在位置\n";
        $str .= "4.点击右上方【发送】按钮\n";
        $str .= "即可获得距离您当前位置最近的东莞国药门店位置啦";
        $this->replyText($str);
	}

	// 上报地理位置事件
	public function location($data) {
        // 其中token和openid这两个参数一定要传，否则程序不知道是哪个微信用户进入了系统
        $param ['token'] = get_token ();
        $param ['openid'] = get_openid ();
//        $url = addons_url ( 'Card://Card/show', $param );
        $startpoint = changepoint(array('x'=>$data['Location_X'], 'y'=>$data['Location_Y']));

        $map['com_md_long'] = array('neq', '');
        $map['com_md_lat'] = array('neq', '');
        $shops = M('com_md')
            ->field('com_zip_name as name, com_md_add as address, com_md_tel as tel, com_md_long as longitude, com_md_lat as latitude')
            ->where($map)->select();

        foreach($shops as &$shop){
            $less_lat= abs($startpoint['x'] - $shop['latitude']);
            $less_lng= abs($startpoint['y'] - $shop['longitude']);;
            $shop['sqrt_sum'] = $less_lat*$less_lat + $less_lng*$less_lng;
        }
        unset($shop);

        for($i=0; $i<(count($shops)-1); $i++){
            $pos = $i;
            $max = $shops[$i+1];
            while(($pos>=0)&&($max['sqrt_sum'] < $shops[$pos]['sqrt_sum']))
            {
                $shops[$pos+1] = $shops[$pos];
                $pos = $pos-1;
            }
            $shops[$pos+1] = $max;
        }
        $endarr = array_slice($shops, 0, 5);
        $articles [0] = array (
            'Title' => '距离您最近的5家门店',
            'Description' => '东莞国药集团',
            'PicUrl' => SITE_URL . '/Addons/StoreGis/View/default/Public/md1.jpg',
            'Url' => ''
        );
        foreach($endarr as $key => &$edata){
            $edata['distance'] = GetDistance($startpoint['x'],$startpoint['y'], $edata['latitude'], $edata['longitude']);

            $articles [$key+1] = array (
                'Title' => $edata['name'] . "\n电话：". $edata['tel'] . "\n距离：" . $edata['distance'] . "米",
                'Description' => $edata['address'],
                'PicUrl' => SITE_URL . '/Addons/StoreGis/View/default/Public/md2.jpg',
                'Url' => 'http://api.map.baidu.com/marker?location=' . $edata['latitude'] . ',' . $edata['longitude'] . '&title=' . $edata['name'] . '&content=' . $edata['address'] . '&output=html&src=dggy'
            );

        }
//        $test['uid'] = $param ['openid'];
//        $test['x'] = $startpoint['x'];
//        $test['y'] = $startpoint['y'];
//        $test['log'] = json_encode($articles);
//        M('testlog')->add($test);

        unset($edata);
        $this->replyNews ( $articles );

	}
}
        	